<template>
    <div style="padding: 10px;">
        <van-nav-bar title="面经详情" left-text="返回" @click-left="$router.back()" />
        <div class="header">
            <h1>标题</h1>
            <p style="font-size: 10px; color: lightgrey;">
                {{ article.createdAt }} | {{ article.views }} 浏览量 |{{ article.likeCount }} 点赞数
            </p>
            <div style="display: flex;">
                <img :src="article.avatar" alt="" style="width: 40px;height: 40px;">
                <div style=" font-size: 15px; line-height: 35px; color: lightgray;">{{ article.creator }}</div>
            </div>
            <div class="text" v-html="article.content" style="font-size: 18px; line-height: 30px;">

            </div>
        </div>
        <div class="opt">
            <van-icon @click="doLike" :class="{ active: article.likeFlag }" name="like-o" />
            <van-icon @click="doCollect" :class="{ active: article.collectFlag }" name="star-o" />
        </div>
    </div>
</template>

<script>
import { getArticleInfo,changeLike,changeCollect } from "@/api/article"
export default {
    name: "detall-page",
    data() {
        return {
            //面经详情
            article: {}
        }
    },
    async created() {
        //发送axios
        const data = await getArticleInfo(this.$route.params.id)
        this.article = data.data
    },
    methods:{
        //切换喜欢
        async doLike(){
            //发送ajax
            await changeLike(this.$route.params.id)
            //(取反)
            this.$toast.success('切换点赞成功')//提示
            this.article.likeFlag=! this.article.likeFlag//取反后点击图标会变色
            //切换点赞修改喜欢人数
            if(this.article.likeFlag){
                this.article.likeCount++
            }else{
                this.article.likeCount--
            }
        },
        async doCollect(){
            //发送ajax
            await changeCollect(this.$route.params.id)
            //(2)
            this.$toast.success('切换收藏成功')//提示

            this.article.collectFlag=! this.article.collectFlag
        }
    }
}
</script>

<style lang="less" scoped>
* {
    margin: 0;
    padding: 0;
}

.opt {
    position: fixed;
    bottom: 100px;
    background-color: #fff;
    right: 0;

    >.van-icon {
        margin-right: 20px;
        background: #fff;
        width: 40px;
        height: 40px;
        line-height: 40px;
        text-align: center;
        border-radius: 50%;
        box-shadow: 2px 2px 10px #ccc;
        font-size: 18px;

        &.active {
            background: #FEC635;
            color: #fff;
        }
    }
}
    </style>